package com.example.myapplication.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

/**
 * @author zijian.cheng
 * @date on 2018/8/2.
 */
public class TimeUtil {

    public static synchronized String getCurrentTime() {
        Random random = new Random();
        int r = random.nextInt(999) % (999 - 100 + 1) + 100;
        return String.valueOf(Math.abs(System.nanoTime())) + r;
    }

    public static Long milliseconds(String dateTime) {
        Calendar c = Calendar.getInstance();

        try {
            c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return c.getTimeInMillis();
    }

    public static Long getSecond(String dateTime) {
        Calendar c = Calendar.getInstance();

        try {
            c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return c.getTimeInMillis() / 1000;
    }

    public static final Long getSecondm(String dateTime) {
        Calendar c = Calendar.getInstance();

        try {
            c.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return c.getTimeInMillis() / 1000;
    }

    public static final String getTimeSSS(long timeMills) {
        return getStatusTimeByTimeMillis(timeMills, "yyyy-MM-dd HH:mm:ss:SSS");
    }

    public static final long getTimeSSS(String dateString) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
        try {
            Date date = df.parse(dateString);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public static final long getTimeSS(String dateString) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            Date date = df.parse(dateString);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }



    public static final String getTimeStandard(long timeMills) {
        return getStatusTimeByTimeMillis(timeMills, "yyyy-MM-dd HH:mm");
    }

    public static final String getTime(long timeMills) {
        return getStatusTimeByTimeMillis(timeMills, "yyyy-MM-dd HH:mm:ss");
    }

    public static final int getHour(long timeMills) {
        return Integer.parseInt(getStatusTimeByTimeMillis(timeMills, "H"));
    }

    public static final int getDay(long timeMills) {
        return Integer.parseInt(getStatusTimeByTimeMillis(timeMills, "dd"));
    }

    public static final int getMM(long timeMills) {
        return Integer.parseInt(getStatusTimeByTimeMillis(timeMills, "mm"));
    }

    public static final String getDailyDate(long timeMills) {
        return getStatusTimeByTimeMillis(timeMills, "yyyy-MM-dd");
    }

    public static final String getImageTime(long timeMills) {
        return getStatusTimeByTimeMillis(timeMills, "MM月dd日");
    }

    public static final String getImageTime2(long timeMills) {
        return getStatusTimeByTimeMillis(timeMills, "yyyy年MM月dd日");
    }

    public static final String getExpireTime(long timeMills) {
        return getStatusTimeByTimeMillis(timeMills, "yyyy.MM.dd到期");
    }

    public static final String getExpireTimeATM(long timeMills) {
        return getStatusTimeByTimeMillis(timeMills, "yyyy.MM.dd HH:mm到期");
    }

    public static final String getImageTakenTime(long timeMills) {
        return getStatusTimeByTimeMillis(timeMills, "yyyy-MM-dd HH:mm:ss");
    }

    public static final String getMonth(long timeMills) {
        return getStatusTimeByTimeMillis(timeMills, "M");
    }


    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();

    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
        }
        return DateLocal.get();
    }


    /**
     * 判断是否为今年(效率比较高)
     *
     * @param date 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今年 false不是
     * @throws ParseException
     */
    public static boolean isThisYear(Date date) {
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        //        Date date = getDateFormat().parse(day);
        cal.setTime(date);
        return cal.get(Calendar.YEAR) == pre.get(Calendar.YEAR);
    }

    /**
     * 判断是否为今天(效率比较高)
     *
     * @param date 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean isToday(Date date) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        //        Date date = getDateFormat().parse(day);
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否为昨天(效率比较高)
     *
     * @param date 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean isYesterday(Date date) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        //        Date date = getDateFormat().parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == -1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否为前天(效率比较高)
     *
     * @param date 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean isExtYesterday(Date date) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        //        Date date = getDateFormat().parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == -2) {
                return true;
            }
        }
        return false;
    }

    public static String formatTimeType(long chatTime) throws ParseException {
        Date date = new Date();
        date.setTime(chatTime);
        if (isToday(date)) {
            return getStatusTimeByTimeMillis(chatTime, "HH:mm");
        } else if (isThisYear(date)) {
            return getStatusTimeByTimeMillis(chatTime, "MM-dd HH:mm");
        } else {
            return getStatusTimeByTimeMillis(chatTime, "yyyy-MM-dd HH:mm");
        }
    }

    public static String formatTime(long chatTime) {
        try {
            return formatTimeType(chatTime);
        } catch (ParseException e) {
            return "";
        }
    }

    public static String getStatusTimeByTimeMillis(long timeMills, String formatType) {
        Date date = new Date();
        date.setTime(timeMills);
        SimpleDateFormat formatter = new SimpleDateFormat(formatType, Locale.CHINA);
        return formatter.format(date);
    }

    public static int compareDate(Date start, Date end) {
        if (null == start || null == end) {
            return Integer.MAX_VALUE;
        }

        return (int) ((end.getTime() - start.getTime()) / (1000 * 3600 * 24));
    }

    public static int compareToToday(Date date) {
        return compareDate(date, new Date());
    }

    public static long compareTimeMin(long time, long extTime) {
        long dur = time - extTime;
        return dur / 1000 / 60;
        //        long eMin = new Timestamp(extTime).getMinutes();
        //        long nMin = new Timestamp(time).getMinutes();
        //        return nMin - eMin;
    }

    public static String getChatTime(long time) {
        long now = System.currentTimeMillis();
        long dur = now - time;

        long min = 0;
        long hour = 0;
        long day = 0;

        if (dur > 0) {
            min = dur / 1000 / 60;
            if (min != 0 && min >= 60) {
                hour = min / 60;
                if (hour != 0 && hour >= 24) {
                    day = hour / 24;
                }
            }
        }
        min = min == 0 ? 1 : min;

        return formatTimeString(min, hour, day, time, false);
    }

    private static String formatTimeString(long min, long hour, long day, long time, boolean withSplit) {
        String split = withSplit ? "  |  " : "";

        String m = split + "%d分钟前";
        String h = split + "%d小时前";
        String d = split + "%d天前";
        if (day != 0) {
            if (day > 30) {
                return split + getStatusTimeByTimeMillis(time, "yyyy-MM-dd");
            } else {
                return String.format(d, day);
            }
        } else if (hour != 0) {
            return String.format(h, hour);
        } else {
            return String.format(m, min);
        }
    }

    public static String getStringMinTime(int recordCnt) {
        int hour = recordCnt / 3600;
        int min = recordCnt % 3600 / 60;
        int second = recordCnt % 60;
        if (hour == 0) {
            return String.format(Locale.CHINA, "%02d:%02d", min, second);
        } else {
            return String.format(Locale.CHINA, "%02d:%02d:%02d", hour, min, second);
        }
    }

    /**
     * 判断两个时间点是否属于同一天
     *
     * @param timeMills      timeMills
     * @param otherTimeMills otherTimeMills
     * @return boolean
     */
    public static boolean isTheSameDay(long timeMills, long otherTimeMills) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        long time1 = Long.parseLong(dateFormat.format(timeMills));
        long time2 = Long.parseLong(dateFormat.format(otherTimeMills));
        if (time1 == time2) {
            return true;
        }
        return false;
    }

    public static String formatHMS(long seconds) {
        long hour = seconds / 60 / 60;
        long minutes = seconds / 60 % 60;
        long remainingSeconds = seconds % 60;
        if (hour > 0) {
            return hour + ":" + minutes + ":" + seconds;
        } else {
            if (minutes > 0) {
                return minutes + ":" + seconds;
            } else {
                return seconds + "";
            }
        }
    }

    /**
     * 获取定时器延时时间
     */
    public static long getTargetTime(long offSetTime, String hourTime) {
        long currentTime = System.currentTimeMillis() - offSetTime;
        String day = TimeUtil.getDailyDate(currentTime);
        long targetTime = TimeUtil.getSecond(day + " " + hourTime);
        long delay = targetTime - currentTime / 1000;
        if (delay < 0) {
            delay += 24 * 60 * 60;
        }
        return delay;
    }
}
